package com.roadjava.like.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.roadjava.like.bean.entity.LikedDO;
import com.roadjava.like.bean.entity.LikedEQ;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface LikedMapper extends BaseMapper<LikedDO> {

    // 添加点赞
    @Insert("INSERT INTO liked (resourceid, id, likestatus) VALUES (#{resourceId}, #{Id}, #{likeStatus})")
    void insert(LikedEQ liked);

    // 删除点赞
    @Delete("DELETE FROM liked WHERE id = #{userId} and resourceid = #{resourceId} and likestatus = 1")
    void removeLike(@Param("resourceId") Long resourceId, @Param("userId") Long userId);

    // 查询用户的所有点赞
    @Select("SELECT * FROM liked WHERE id = #{userId}")
    List<LikedDO> getUserLikes(@Param("userId") Long userId);

    // 查询指定资源的点赞数量
    @Select("SELECT COUNT(*) FROM liked WHERE resourceid = #{resourceId} AND likestatus = 1")
    Long getLikeCount(@Param("resourceId") Long resourceId);

    @Select("SELECT COUNT(*) > 0 FROM liked WHERE id = #{userId} AND resourceid = #{resourceId} AND likestatus = 1")
    boolean existsByUserIdAndResourceId(@Param("userId") Long userId, @Param("resourceId") Long resourceId);
}
